﻿using AutoMapper;
using Inventorycount.API.Write.Application.Command;
using Inventorycount.Domain;
using Inventorycount.ErrorCode;
using Inventorycount.Infrastructure;
using MediatR;

namespace Inventorycount.API.Write.Application.CommandHandLer
{
    public class UpdateTransferCommandHandLer : IRequestHandler<UpdateTransferCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ILogger<UpdateTransferCommandHandLer> logger;
        private readonly SqlSugarHelp<Wms_Transfer_detail> detail;
        private readonly SqlSugarHelp<Wms_Transfer_order> order;
        private readonly SqlSugarHelp<Wms_Operation_log> logService;
        public UpdateTransferCommandHandLer(SqlSugarHelp<Wms_Transfer_detail> detail, SqlSugarHelp<Wms_Transfer_order> order, IMapper mapper, ILogger<UpdateTransferCommandHandLer> logger, SqlSugarHelp<Wms_Operation_log> logService)
        {
            this.detail = detail;
            this.order = order;
            this.mapper = mapper;
            this.logger = logger;
            this.logService = logService;
        }
        public async Task<APIResult<int>> Handle(UpdateTransferCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                // 更新主表 Material
                var material = mapper.Map<Wms_Transfer_order>(request);
                material.UpdateDateTime = DateTime.Now;
                var updateResult = await order.UpdateAsync(material);
                if (updateResult <= 0)
                {
                    result.Code = ResultCode.Fail;
                    result.Message = "调拨修改失败";
                    return result;
                }

                // 更新子表 MaterialDetail
                var materialDetails = mapper.Map<List<Wms_Transfer_detail>>(request.Detail);
                foreach (var detail in materialDetails)
                {
                    detail.Transfer_id = request.Id;
                }
                var detailUpdateResult = await detail.UpdateRange(materialDetails);

                if (detailUpdateResult <= 0)
                {
                    result.Code = ResultCode.Fail;
                    result.Message = "调拨详情修改失败";
                    return result;
                }

                result.Code = ResultCode.Ok;
                result.Message = "调拨及调拨详情修改成功";

                 var log = new Wms_Operation_log
                    {
                        Transfer_id = material.Id,
                        Operation_type = "修改",
                        operator_id = material.Operator_id,
                        Operation_desc = $"创建调拨单，调出仓库:{material.Out_warehouse_id}，调入仓库:{material.In_warehouse_id}",
                        operation_time = DateTime.Now,

                    };
                    await logService.AddAsync(log);
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = ex.Message;
                logger.LogError("出现异常: " + ex.Message);
                throw ex;
            }

            return await Task.FromResult(result);
        }
    
    }
}
